<template>
	<view class="page">
		<p class="welcome">欢迎使用~</p>
		<p class="login-title">用户登录</p>
		<view class="login-form">
			<view class="form-item">
				<input type="text" placeholder="请输入手机号" maxlength="11" auto-focus v-model="phoneNum"/>
			</view>
			<view class="form-item">
				<input type="password" placeholder="请输入密码" maxlength="11" v-model="password"/> 
			</view>
			<p>我已阅读并同意<span>用户手册</span>和<span>隐私政策</span></p>
		</view>
		<view class="login-btn" @click="loginForm">
			立即登录
		</view>
		<view class="go-register">
			<p>还没账号？</p>
			<span @click="goRegister">立即注册</span>
		</view>
	</view>
</template>

<script>
	import {login} from "../../api/index.js"
	export default {
		data() {
			return {
				phoneNum:'',
				password:''
			}
		},
		methods: {
			goRegister(){
				uni.navigateTo({
					url:'/pages/register/register'
				})
			},
			loginForm(){
				//组合数据
				let obj = {
					phoneNum:this.phoneNum,
					password:this.password
				}
				console.log(obj);
				//判断输入的是否为空
				if(!this.phoneNum.trim() || !this.password.trim()){
					uni.showToast({
						title:"不能为空"
					})
					return
				}
				login(obj).then(res=>{
					if(res.data.status === 0){
						console.log(res,"登录返回的数据");
						uni.showToast({
							title:"登录成功"
						})
						//跳转页面
						setTimeout(()=>{
							//跳转主页
							uni.switchTab({
								url:"/pages/index/index"
							})
						},800)	
						//把返回的token和userInfo存到本地 使用uni.setStorageSync
						uni.setStorageSync("name","刘帅龙")
						uni.setStorageSync("token",res.data.token)
						uni.setStorageSync("userInfo",res.data.userInfo)
					}
					else{
						uni.showToast({
							title:"登录失败"
						})
					}
				})
				//提交数据
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		background:linear-gradient(#B2D0EC,#FBFCFE);
		width:100%;
		height: 100vh;
		.welcome{
			font-size: 36rpx;
			font-weight: bold;
			width: 100%;
			height: 40rpx;
			line-height: 40rpx;
			padding-left: 15rpx;
		}
		.login-title{
			font-size: 34rpx;
			font-weight: bold;
			width: 100%;
			height:40rpx;
			line-height: 40rpx;
			padding-left: 15rpx;
			margin: 120rpx 0 20rpx 0;
		}
		.login-form{
			width: 90%;
			height: 300rpx;
			margin: 0 auto;
			// background-color: #fff;
			padding: 10rpx;
			.form-item{
				height: 100rpx;
				// background-color: pink;
				margin: 20rpx 0;
			}
			input{
				background-color: #ffffff;
				padding-left: 5rpx;
				height: 80rpx;
				border-radius: 50rpx;
			}
			p{
				padding-top: 10px;
				font-size: 28rpx;
			}
			span{
				color: #A7C5FB;
			}
		}
		.login-btn{
			width: 85%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background-color: #A7C5FB;
			margin: 200rpx auto 0;
			border-radius: 80rpx;
		}
		.go-register{
			display: flex;
			// width: 100%;
			justify-content: center;
			height: 20rpx;
			margin-top: 40rpx;
			line-height: 20rpx;
			// background-color: pink;
			p{
				text-align: center;
				font-size: 28rpx;
			}
			span{
				font-size: 28rpx;
				color: #A7C5FB;
			}
		}
		
}

</style>
